package com.first.config.mybatis;

import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.first.config.druid.DruidConfiguration;
import com.github.pagehelper.PageInterceptor;
@Configuration
//@EnableTransactionManagement
//@MapperScan("com.first.springBoot.service.impl")
public class MyBatisConfig implements TransactionManagementConfigurer{
	
	  @Autowired
	  private Environment env;
	
	  @Autowired
	  private DruidConfiguration DruidConfiguration;
	
	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
		fb.setDataSource(DruidConfiguration.dataSource());// 指定数据源(这个必须有，否则报错)
		
//	        //添加插件  
//	     MySelfInterceptor MySelfInterceptor = new MySelfInterceptor();
	     fb.setPlugins(new Interceptor[] { PageInterceptor()});
		
		// 下边两句仅仅用于*.xml文件，如果整个持久层操作不需要使用到xml文件的话（只用注解就可以搞定），则不加
		fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));// 指定基包
		fb.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));// 指定xml文件位置
		return fb.getObject();
	}

	
	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
	return new SqlSessionTemplate(sqlSessionFactory);
	}
	@Bean
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
	return new DataSourceTransactionManager(DruidConfiguration.dataSource());
	}
	
	
	@Bean
	public PageInterceptor PageInterceptor() {
		PageInterceptor pageHelper = new PageInterceptor();  
	     Properties props = new Properties();  
	     props.setProperty("reasonable", "true");  
	     props.setProperty("supportMethodsArguments", "true");  
	     props.setProperty("returnPageInfo", "check");  
	     props.setProperty("params", "count=countSql");  
	     pageHelper.setProperties(props);
		return pageHelper;
	}
	/**
	 * SqlSessionFactory拦截器测试
	 * @return
	 */
	@Bean
	public MySelfInterceptor MySelfInterceptor() {
		MySelfInterceptor MySelfInterceptor = new MySelfInterceptor();  
	     Properties props = new Properties();  
	     props.setProperty("reasonable", "true");  
	     props.setProperty("supportMethodsArguments", "true");  
	     props.setProperty("returnPageInfo", "check");  
	     props.setProperty("params", "count=countSql");  
	     MySelfInterceptor.setProperties(props);
		return MySelfInterceptor;
	}
	
	
}
